package com.webchat.admin.controller.sys;


import com.webchat.admin.service.sys.SysDataDictionaryService;
import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.ValidatePermission;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.sys.SysDataDictionaryDetailRequestVo;
import com.webchat.domain.vo.request.sys.SysDataDictionaryRequestVO;
import com.webchat.domain.vo.response.sys.SysDataDictionaryDetailBaseVO;
import com.webchat.domain.vo.response.sys.SysDataDictionaryResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 数据字典控制器
 *
 * @author SongRenShuo
 * @date 2025/06/28
 */
@RestController
@RequestMapping("/admin-service/sys/data-dictionary")
public class SysDataDictionaryController {

    @Autowired
    private SysDataDictionaryService sysDataDictionaryService;

    /**
     * 创建或者更新数据字典
     *
     * @param requestVO 请求 VO
     * @return {@link APIResponseBean }<{@link Long }>
     */
    @ValidatePermission
    @PostMapping("/save")
    public APIResponseBean<Long> save(@RequestBody SysDataDictionaryRequestVO requestVO) {

        String userId = SessionHelper.getCurrentUserId();
        requestVO.setOperator(userId);
        Long id = sysDataDictionaryService.save(requestVO);
        return APIResponseBeanUtil.success(id);
    }

    /**
     * 删除数据字典
     *
     * @param dictionaryId 数据字典 ID
     * @return {@link APIResponseBean }<{@link Long }>
     */
    @ValidatePermission
    @PostMapping("/delete/{dictionaryId}")
    APIResponseBean<Long> delete(@PathVariable Long dictionaryId) {

        String userId = SessionHelper.getCurrentUserId();
        boolean res = sysDataDictionaryService.delete(dictionaryId, userId);
        return APIResponseBeanUtil.success(res);
    }


    /**
     * 分页查询数据字典
     *
     * @param categoryId     分类 ID
     * @param code     编号
     * @param name     名称
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return {@link APIPageResponseBean }<{@link SysDataDictionaryResponseVO }>
     */
    @ValidatePermission
    @GetMapping("/page")
    public APIPageResponseBean<SysDataDictionaryResponseVO> page(@RequestParam(value = "categoryId", required = false) Long categoryId,
                                                                 @RequestParam(value = "code", required = false) String code,
                                                                 @RequestParam(value = "name", required = false) String name,
                                                                 @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        return sysDataDictionaryService.page(categoryId,code, name, pageNo, pageSize);
    }
/**
     * 创建或者更新数据字典详情
     *
     * @param requestVO 请求 VO
     * @return {@link APIResponseBean }<{@link Long }>
     */
    @ValidatePermission
    @PostMapping("/saveDetail")
    public APIResponseBean<Long> saveDetail(@RequestBody SysDataDictionaryDetailRequestVo requestVO) {

        String userId = SessionHelper.getCurrentUserId();
        requestVO.setOperator(userId);
        Long id = sysDataDictionaryService.saveDetail(requestVO);
        return APIResponseBeanUtil.success(id);
    }

    /**
     * 删除数据字典详情
     *
     * @param detailId 数据字典 ID
     * @return {@link APIResponseBean }<{@link Long }>
     */
    @ValidatePermission
    @PostMapping("/deleteDetail/{detailId}")
    APIResponseBean<Long> deleteDetail(@PathVariable Long detailId) {

        String userId = SessionHelper.getCurrentUserId();
        boolean res = sysDataDictionaryService.deleteDetail(detailId, userId);
        return APIResponseBeanUtil.success(res);
    }


    /**
     * 分页查询数据字典详情
     *
     * @param dataDictionaryId     数据字典ID
     * @param code     编号
     * @param name     名称
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return {@link APIPageResponseBean }<{@link SysDataDictionaryDetailBaseVO }>
     */
    @ValidatePermission
    @GetMapping("/pageDetail")
    public APIPageResponseBean<SysDataDictionaryDetailBaseVO> pageDetail(@RequestParam(value = "dataDictionaryId") Long dataDictionaryId,
                                                                         @RequestParam(value = "code", required = false) String code,
                                                                         @RequestParam(value = "name", required = false) String name,
                                                                         @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        return sysDataDictionaryService.pageDetail(dataDictionaryId,code, name, pageNo, pageSize);
    }


}
